package com.voltmao.service.application.module;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.voltmao.persistent.mapper.AdminMenuFunMapper;
import com.voltmao.persistent.mapper.AdminMenuMapper;
import com.voltmao.persistent.mapper.SysDictionaryMapper;
import com.voltmao.persistent.mapper.SysUserInfoMapper;
import com.voltmao.persistent.model.SysDictionaryCriteria;
import com.voltmao.persistent.model.SysUserInfo;
import com.voltmao.persistent.model.SysUserInfoCriteria;
import com.voltmao.webpro.frame.dto.DataPage;
import com.voltmao.webpro.utils.MD5Utils;
import com.voltmao.webpro.utils.ParamsUtil;
import com.voltmao.webpro.utils.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * 系统用户服务接口
 *
 * @author zhouyang
 * @date 2018/3/30
 */
@Service
public class SysUserInfoServiceImpl {
    @Autowired
    AdminMenuMapper adminMenuMapper;
    @Autowired
    AdminMenuFunMapper adminMenuFunMapper;
    @Autowired
    SysDictionaryMapper sysDictionaryMapper;
    @Autowired
    SysUserInfoMapper sysUserInfoMapper;

    /**
     * 用户分页数据及查询
     *
     * @param params
     * @return
     */
    public DataPage selectSysUsersDataPage(Map<String, Object> params) {
        DataPage dataPage = new DataPage();

        int pageindex = StringUtils.parseInt(params.get("pageNumber") + "", 1);
        int pagesize = StringUtils.parseInt(params.get("pageSize") + "", 20);

        SysUserInfoCriteria example = new SysUserInfoCriteria();

        example.setPageSize(pagesize);
        example.setLimitStart((pageindex - 1) * pagesize);

        if (params != null && params.size() > 0) {
            SysUserInfoCriteria.Criteria c = example.createCriteria();


        }


        dataPage.pageindex = pageindex;
        dataPage.pagesize = pagesize;
        dataPage.rows = sysUserInfoMapper.selectMapByExample(example);
        dataPage.total = sysUserInfoMapper.countByExample(example);

        return dataPage;
    }

    /**
     * 登录初始化数据
     *
     * @param sysUserId
     * @return
     */
    public Map<String, Object> getMenuData(String sysUserId) {
        Map<String, Object> result = new HashMap<String, Object>();

        //查询菜单组
        result.put("menuRules", adminMenuMapper.selectMenusForSysUserId(sysUserId));

        //查询功能组
        result.put("funRules", adminMenuFunMapper.selectFunsForSysUserId(sysUserId));

        //查询用户
        result.put("userInfo", sysUserInfoMapper.getLoginInfo(sysUserId));

        //查询字典组
        SysDictionaryCriteria example = new SysDictionaryCriteria();
        example.setOrderByClause("sort_num");
        result.put("dicJson", sysDictionaryMapper.selectByExample(example));
        SysDictionaryCriteria.Criteria c = example.createCriteria();
        c.andPcodeEqualTo("fileServiceUrl");
        result.put("fileServiceUrl",sysDictionaryMapper.selectByExample(example));
        return result;
    }

    /**
     * 验证登录
     *
     * @param params
     * @return
     */
    public SysUserInfo authLogin(Map<String, Object> params) {
        SysUserInfoCriteria example = new SysUserInfoCriteria();
        SysUserInfoCriteria.Criteria c = example.createCriteria();
        c.andUserNameEqualTo(params.get("username") + "");
        c.andPwdEqualTo(MD5Utils.getPwd(params.get("password")+""));
        SysUserInfo sysUserInfo = sysUserInfoMapper.selectByExampleForOne(example);

        return sysUserInfo;
    }

    /**
     * 员工修改密码
     * @param params
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public int updatePwd(Map<String, Object> params){
        //检查参数完整性
        String[] checkParams = {"dataId","newPassword","oldPassword"};
        ParamsUtil.checkNotNull(params,checkParams);
        //检查原始密码是否正确
        SysUserInfo sysUserInfo = sysUserInfoMapper.selectByPrimaryKey(params.get("dataId")+"");
        String originPwd = sysUserInfo.getPwd();
        if(!originPwd.equals(MD5Utils.getPwd(params.get("oldPassword")+""))){
            throw new RuntimeException("原始密码不正确");
        }
        sysUserInfo.setPwd(MD5Utils.getPwd(params.get("newPassword")+""));
        return sysUserInfoMapper.updateByPrimaryKeySelective(sysUserInfo);
    }


}
